﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using HYRevitCode.FormUtility;

namespace YArchitech.Plumbing
{
	public partial class FormCleanoutSetting : System.Windows.Forms.Form
	{
		private void form_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.Icon = Resource.YArchitechIcon;
		}

		public FormCleanoutSetting(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{381ADDE5-2C10-4F03-BC4E-ACE58CE7A9D4}", null);
			this.doc = doc;
			string autoDesignSetting = this.userSetting.GetAutoDesignSetting("layCleanoutType");
			string autoDesignSetting2 = this.userSetting.GetAutoDesignSetting("layCleanoutOffSet");
			if (autoDesignSetting == "0")
			{
				this.rdoFloor.Checked = true;
			}
			else
			{
				this.rdoPipe.Checked = true;
			}
			this.textbox.Text = ((autoDesignSetting2.Trim() == "") ? "0" : autoDesignSetting2);
		}

		private double getLevelOffset(string level)
		{
			return this.Get_Level(this.doc, level).ProjectElevation;
		}

		private Level Get_Level(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Level), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as Level;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private double zCoord(double offset)
		{
			return offset / 0.3048 / 1000.0;
		}

		private void win_deactivate(object sender, EventArgs e)
		{
			this.heigh = this.zCoord(double.Parse((this.textbox.Text == "") ? "0" : this.textbox.Text));
			if (this.rdoFloor.Checked)
			{
				this.setReference = 0;
			}
			else if (this.rdoPipe.Checked)
			{
				this.setReference = 1;
			}
			string nodeValue = this.textbox.Text.Trim();
			this.userSetting.WriteAutoDesignSetting("layCleanoutOffSet", nodeValue);
		}

		private void rdoFloor_CheckedChanged(object sender, EventArgs e)
		{
			this.userSetting.WriteAutoDesignSetting("layCleanoutType", "0");
		}

		private void rdoPipe_CheckedChanged(object sender, EventArgs e)
		{
			this.userSetting.WriteAutoDesignSetting("layCleanoutType", "1");
		}

		private void FormCleanoutSetting_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		public double heigh;

		private Document doc;

		public int setReference;

		private ControlInfoManager m_thisManger;
	}
}
